const originalXHR = window.XMLHttpRequest;

window.XMLHttpRequest = function() {
const xhr = new originalXHR();

// 重写onreadystatechange以捕获响应
const originalOnReadyStateChange = xhr.onreadystatechange;
xhr.onreadystatechange = function() {
  if (xhr.readyState === 4) { // 请求完成
    if (xhr.status === 200) {
        if (xhr.responseURL.indexOf("https://mobile.yangkeduo.com/proxy/api/api/galen/v2/regions") != -1) {
            document.body.setAttribute('data-regions', xhr.responseText);    
        }
      // chrome.runtime.sendMessage({
      //   type: 'xhr_data',
      //   url: xhr.responseURL,
      //   data: xhr.responseText
      // });
    }
  }
  if (originalOnReadyStateChange) {
    originalOnReadyStateChange.apply(xhr, arguments);
  }
};

return xhr;
};

(async function() {

	const p = '浙江省'; //省
	const c = '温州市';
	const d = '苍南县';
	const moren = document.getElementsByClassName('_2PiBMVlL');
	if (moren.length == 0) {
		alert("没有找到默认地址");
		return;
	}
	moren[0].parentElement.nextElementSibling.querySelector('div._3S2zeKn-').click();

	await new Promise(resolve => 
        setTimeout(() => {
            resolve();
        }, 500)
    );

    // document.getElementById('name').value = '陈文越';
    document.querySelector("span.m-addr-select").click();
    await new Promise(resolve => 
        setTimeout(() => {
            resolve();
        }, 500)
    );
    // 省
    const plis = document.getElementById('region-selector-list-1').children;
    for (var i = 0; i < plis.length; i++) {
    	const p_html = plis[i].children[0].textContent;
    	if (p_html == p) {
    		plis[i].click();
    		break;
    	}
    }

    await new Promise(resolve => 
        setTimeout(() => {
            resolve();
        }, 500)
    );

    // 市
    const clis = document.getElementById('region-selector-list-2').children;
    for (var i = 0; i < clis.length; i++) {
    	const c_html = clis[i].children[0].textContent;
    	if (c_html == c) {
    		clis[i].click();
    		break;
    	}
    }

    await new Promise(resolve => 
        setTimeout(() => {
            resolve();
        }, 500)
    );

    // 区
    const dlis = document.getElementById('region-selector-list-3').children;
    for (var i = 0; i < dlis.length; i++) {
    	const d_html = dlis[i].children[0].textContent;
    	if (d_html == d) {
    		dlis[i].click();
    		break;
    	}
    }

    await new Promise(resolve => 
        setTimeout(() => {
            resolve();
        }, 500)
    );

    document.body.setAttribute("data-ready", 0);
    // document.querySelector("div.m-addr-save-new").click();

})();